<template>
  <div class="main-container">
    <div class="main-content">
      <div class="row">
        <div class="col-sm-10 col-sm-offset-1">
          <div class="login-container">
            <div class="center">
              <h1>
                <i class="ace-icon fa fa-leaf green"></i>
                <span class="white" id="id-text2">后台登录</span>
              </h1>
            </div>

            <div class="space-6"></div>

            <div class="position-relative">
              <div id="login-box" class="login-box visible widget-box no-border">
                <div class="widget-body">
                  <div class="widget-main">
                    <h4 class="header blue lighter bigger">
                      <i class="ace-icon fa fa-coffee green"></i>
                      请输入用户名密码
                    </h4>

                    <div class="space-6"></div>

                    <form>
                      <fieldset>
                        <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input v-model="user.loginName" type="text" class="form-control" placeholder="用户名"/>
															<i class="ace-icon fa fa-user"></i>
														</span>
                        </label>

                        <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input v-model="user.password" type="password" class="form-control" placeholder="密码"/>
															<i class="ace-icon fa fa-lock"></i>
														</span>
                        </label>

                        <label class="block clearfix">
                          <span class="block input-icon input-icon-right">
                            <div class="input-group">
                              <input v-model="user.imageCode" type="text" class="form-control" placeholder="验证码">
                              <span class="input-group-addon" id="basic-addon2">
                                <img v-on:click="loadImageCode" id="image-code" alt="验证码"/>
                              </span>
                            </div>
                          </span>
                        </label>

                        <div class="space"></div>

                        <div class="clearfix">
                          <label class="inline">
                            <input v-model="remember" type="checkbox" class="ace"/>
                            <span class="lbl">记住我</span>
                          </label>

                          <button type="button" @click="login" class="width-35 pull-right btn btn-sm btn-primary">
                            <i class="ace-icon fa fa-key"></i>
                            <span class="bigger-110">登录</span>
                          </button>
                        </div>
                        <div class="space-4"></div>
                      </fieldset>
                    </form>
                  </div><!-- /.widget-main -->
                </div><!-- /.widget-body -->
              </div><!-- /.login-box -->
            </div><!-- /.position-relative -->
          </div>
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.main-content -->
  </div><!-- /.main-container -->
</template>

<script>
import {onMounted, ref} from "vue";
import {useRouter} from 'vue-router';
import axios from "axios";

export default {
  name: 'Login',
  setup() {
    const route = useRouter();
    const user = ref({}); // 登录用户信息
    const remember = ref(true); // 默认记住密码

    /**
     * 用户登录
     */
    const login = () => {
      let md5 = hex_md5(user.value.password + KEY);
      let rememberUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER) || {};
      if (rememberUser.md5 !== md5) {
        // 密码加密
        user.value.password = hex_md5(user.value.password + KEY);
      }

      Loading.show();
      axios.post(process.env.VUE_APP_SERVER + "/system/admin/user/login", user.value).then((response) => {
        const resp = response.data;
        if (resp.success) {
          Loading.hide();
          route.push({path: "/welcome"});
          // 保存登录信息
          Tool.setLoginUser(resp.content);
          // 记住密码
          if (remember.value) {
            // 对密码二次加密 判断密码是否修改过
            let md5 = hex_md5(user.value.password + KEY);
            LocalStorage.set(LOCAL_KEY_REMEMBER_USER, {
              loginName: resp.content.loginName,
              password: user.value.password,
              md5: md5
            });
          } else {
            // 清除记住的密码记录
            LocalStorage.remove(LOCAL_KEY_REMEMBER_USER);
            remember.value = false;
          }
        } else {
          Loading.hide();
          Toast.warning(resp.message);
        }
      })
    }

    /**
     * 加载验证码图片
     */
    const loadImageCode = () => {
      let imageCodeToken = Tool.uuid(8, 64); // 8位64进制
      user.value.imageCodeToken = imageCodeToken;
      // 获取图片
      $('#image-code').attr('src', process.env.VUE_APP_SERVER + "/system/admin/kaptcha/image-code/" + imageCodeToken);
    }

    onMounted(() => {
      $('body').removeClass('no-skin');
      $('body').attr('class', 'login-layout light-login');

      // 获取记住的密码
      if (remember.value){
        user.value = LocalStorage.get(LOCAL_KEY_REMEMBER_USER) || {};
      }

      loadImageCode(); // 加载验证码图片
    })

    return {
      login,
      user,
      remember,
      loadImageCode
    }
  }
}
</script>

<style scoped>
 .input-group-addon {
   padding: 0;
 }
</style>